package com.zcsy.manage.web.news;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.model.RequestConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.commons.util.DateUtils;
import com.zcsy.manage.model.base.CellInfo;
import com.zcsy.manage.model.news.CellManual;
import com.zcsy.manage.model.system.Dict;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.service.base.ICellInfoService;
import com.zcsy.manage.service.news.ICellManualService;
import com.zcsy.manage.service.system.IDictService;
import com.zcsy.manage.web.common.LogUtils;

/**
 * 
 * @Title: CellManualController.java
 * @Package： com.zcsy.manage.web.news
 * @Description: 社区政务控制层
 *
 * @author lilan 1063972142@qq.com
 * @Copyright: 2015 武汉恒数利通信息技术有限公司 All rights reserved.
 * @date 2016年6月6日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Controller
@RequestMapping(value = "/cellManual")
public class CellManualController {
	@Autowired
	private ICellManualService cellManualService;

	@Autowired
	private ICellInfoService cellInfoService;

	@Autowired
	private IDictService dictService;
	
	/**
	 * @Description 返回社区政务查询列表<br>
	 * 
	 * @param model
	 * @param pNo
	 *            当前页面
	 * @param title
	 *            标题
	 * @param req
	 *            用户请求
	 * @return 返回社区政务查询列表
	 * @author lilan 1063972142@qq.com
	 * @date 2016年3月30日
	 * @see
	 */
	@RequestMapping(value = "cellManualList", method = RequestMethod.GET)
	public String cellManualList(Model model, Integer pNo, Integer pSize, 
			String title, HttpServletRequest req) {

		if (pNo == null) {
			pNo = Constants.PAGE_NO;
		}

		Integer pageSize;
		if (pSize == null) {
			pageSize = Constants.PAGE_SIZE;
		} else {
			switch (pSize) {
				case 0: pageSize = Constants.PAGE_SIZE;
				break;
				case 1: pageSize = Constants.PAGE_SIZE_TWENTY;
				break;
				case 2: pageSize = Constants.PAGE_SIZE_FIFTY;
				break;
				default:pageSize = Constants.PAGE_SIZE;
				break;
			}
		}
		
		// 此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
		PageHelper.startPage(pNo, pageSize);

		CellManual cellManual = new CellManual();

		//获取当前小区id
        User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
        String cellId = user.getCurrentCell().getId();
		cellManual.setCellId(cellId);

		if (StringUtils.isNotEmpty(title)) {
			cellManual.setTitle(title);
		}

		List<CellManual> cellManuals = cellManualService
				.selectByKeyword(cellManual);
		PageInfo<CellManual> manual = new PageInfo<CellManual>(cellManuals);

		model.addAttribute("cellManual", manual);
		model.addAttribute("currentPage", pNo);
		model.addAttribute("pSize", pSize);
		model.addAttribute("title", title);

		List<Dict> pageSizes = dictService.getDictFromCache("pageSize");
		model.addAttribute("pageSizes", pageSizes);

		return "news/cellmanual/cellManualList";
	}

	/**
	 * 
	 * @Description 返回新增或编辑小区页面<br>
	 *              type = 1:新增页面 type = 2:编辑页面
	 * 
	 * @param model
	 * @param id
	 *            主键id id=0新增 ；id!=0编辑
	 * @return 返回新增或编辑小区页面
	 * @author lilan 1063972142@qq.com
	 * @date 2016年3月31日
	 * @see
	 */
	@RequestMapping(value = "editCellManual", method = RequestMethod.GET)
	public String selectById(Model model, String id) {
		try {
			if (String.valueOf(Constants.STATE_DISABLED).equals(id)) {
				model.addAttribute("type", Constants.SAVE_TYPE);
			} else {
				CellManual cellManual = cellManualService
						.selectByPrimaryKey(id);
				if (cellManual != null) {
					model.addAttribute("type", Constants.EDIT_TYPE);
					model.addAttribute("cellManual", cellManual);
					model.addAttribute("manualcellId", cellManual.getCellId());
				}
			}
			
		} catch (Exception e) {
		}

		return "news/cellmanual/cellManualEdit";
	}

	/**
	 * @Description 返回社区政务详情页面<br>
	 * @param model
	 * @param id  主键id
	 * @author lilan 1063972142@qq.com
	 * @date 2016年5月5日
	 * @see
	 */
	@RequestMapping(value = "cellManualDetail", method = RequestMethod.GET)
	public String cellManualDetail(Model model, String id) {
		try {
			if (String.valueOf(Constants.STATE_DISABLED).equals(id)) {
				model.addAttribute("type", Constants.SAVE_TYPE);
			} else {
				CellManual cellManual = cellManualService
						.selectByPrimaryKey(id);
				if (cellManual != null) {
					model.addAttribute("type", Constants.EDIT_TYPE);
					model.addAttribute("cellManual", cellManual);
					model.addAttribute("manualcellId", cellManual.getCellId());
				}
			}
			List<CellInfo> cellInfos = cellInfoService.getAllCellInfo();
			model.addAttribute("cellInfos", cellInfos);

		} catch (Exception e) {
		}

		return "news/cellmanual/cellManualDetail";
	}

	/**
	 * @Description 保存和更新社区政务方法<br>
	 *              d不为空且不为0，则表示更新 否则表示保存
	 * 
	 * @param title
	 *            标题
	 * @param subtitle
	 *            副标题
	 * @param keyword
	 *            关键字
	 * @param content
	 *            内容
	 * @param account
	 *            发布人账号
	 * @param id
	 *            当前记录id
	 * @param cellid
	 *            当前记录小区id
	 * @param memo
	 *            备注
	 * @param req
	 *            用户请求
	 * @return 返回结果1：成功 0： 失败
	 * @author lilan 1063972142@qq.com
	 * @date 2016年3月31日
	 * @see
	 */
	@RequestMapping(value = "saveOrUpdateCellManual", method = RequestMethod.POST)
	@ResponseBody
	public RequestJson saveOrUpdate(String title, String subtitle,
			String keyword, String content, String account, String id,
			String logo, Integer sort,
			HttpServletRequest req) {
		RequestJson result = new RequestJson();
		CellManual cellManual = new CellManual();
		User user = (User) req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
		String cellId = user.getCurrentCell().getId();
		cellManual.setTitle(title);
		cellManual.setSubTitle(subtitle);
		cellManual.setContent(content);
		cellManual.setKeyWord(keyword);
		cellManual.setCellId(cellId);
		cellManual.setSort(sort);
		cellManual.setLogo(logo);
		cellManual.setUserAccount(user.getLoginname());
		// cellManual.setMemo(memo);

		try {
			if (id != "" && !"0".equals(id)) {
				cellManual.setId(id);
				cellManual.setUpdateTime(new Date());
				cellManualService.update(cellManual);
				LogUtils.insertLog("用户更新社区政务", user.getId(), req);

			} else {
				cellManual.setId(UniqueNumberUtils.get18UniqueNumber());
				cellManual.setSendTime(new Date());
				cellManual.setUpdateTime(new Date());
				cellManualService.insert(cellManual);
				LogUtils.insertLog("用户新增社区政务", user.getId(), req);

				// 插入一条区域，将当前区域所在的小区绑定依赖关系，有依赖的小区不能被删除
				CellInfo cellInfo = cellInfoService.selectByPrimaryKey(cellId);
				cellInfo.setIsUsed(Constants.IS_USED);
				cellInfoService.update(cellInfo);
			}
			result.setSuccess(true);
		} catch (Exception e) {
			result.setMsg("操作失败");
		}

		return result;
	}

	/**
	 * @Description 根据社区政务id可以同时删除多条政务消息<br>
	 * 
	 * @param ids
	 *            社区政务对象id数组
	 * @param req
	 *            用户请求
	 * @return 删除结果RequestJson
	 * @author lilan 1063972142@qq.com
	 * @date 2016年3月30日
	 * @see
	 */
	@RequestMapping(value = "deleteByPrimaryKey", method = RequestMethod.POST)
	@ResponseBody
	public RequestJson deleteByPrimaryKey(String ids, HttpServletRequest req) {

		RequestJson result = new RequestJson();
		User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);

		try {
			String keys[] = ids.split("\\,");
			for (int i = 0; i < keys.length; i++) {
				cellManualService.deleteByPrimaryKey(keys[i]);
			}
			result.setSuccess(true);
			LogUtils.insertLog("用户删除社区账务信息", user.getId(), req);

		} catch (NumberFormatException e) {
			result.setSuccess(false);
			result.setMsg("操作失败");
		}

		return result;
	}

	/**
	 * @Description 将选中项置顶
	 * @param id
	 *            主键id
	 * @param req
	 *            用户请求
	 * @return 响应结果
	 * @author liuying 1075817474@qq.com
	 * @date 2016年6月12日
	 * @see
	 */
	@RequestMapping(value = "updateSort", method = RequestMethod.GET)
	@ResponseBody
	public RequestJson updateSort(String id, HttpServletRequest req) {
		RequestJson result = new RequestJson();
		User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);

		try {
			CellManual cellManual = cellManualService.selectByPrimaryKey(id);
			if (cellManual != null) {
				cellManual.setUpdateTime(DateUtils.getDate());
				cellManualService.updateSort(cellManual);
				LogUtils.insertLog("用户置顶社区政务", user.getId(), req);

			}
		} catch (Exception e) {
			result.setMsg("操作失败");
			result.setSuccess(false);
		}

		return result;
	}
}
